package com.bocai.eelly.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.activitys.ChoosePhotosActivity;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.bean.DialogBean;
import com.bocai.eelly.common.BaseFragment;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.ApiManage;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;

public class PostClothingStockFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.ll_choose_style_name)
    LinearLayout llChooseStyleName;
    @Bind(R.id.tv_choose_style_name)
    TextView tvChooseStyleName;
    @Bind(R.id.et_post_cloth_stock_brand)
    EditText etPostClothStockBrand;
    @Bind(R.id.et_post_cloth_stock_fabric)
    EditText etPostClothStockFabric;
    @Bind(R.id.et_post_cloth_stock_size)
    EditText etPostClothStockSize;
    @Bind(R.id.et_post_cloth_stock_color)
    EditText etPostClothStockColor;
    @Bind(R.id.et_post_cloth_stock_price)
    EditText etPostClothStockPrice;
    @Bind(R.id.et_post_cloth_stock_num)
    EditText etPostClothStockNum;
    @Bind(R.id.et_post_cloth_stock_country)
    EditText etPostClothStockCountry;
    @Bind(R.id.tv_post_cloth_stock_time)
    TextView tvPostClothStockTime;
    @Bind(R.id.ll_post_cloth_stock_time)
    LinearLayout llPostClothStockTime;
    @Bind(R.id.et_post_cloth_stock_backup)
    EditText etPostClothStockBackup;
    @Bind(R.id.ll_post_cloth_stock_choose_photo)
    LinearLayout llPostClothStockChoosePhoto;
    @Bind(R.id.tv_post_cloth_stock_commit)
    TextView tvPostClothStockCommit;
    private View view;

    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_post_cloth_stock, null);
        initView();
        return view;

    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }
    private void initView() {

        ButterKnife.bind(this, view);
        llChooseStyleName.setOnClickListener(this);
        llPostClothStockChoosePhoto.setOnClickListener(this);
        llPostClothStockTime.setOnClickListener(this);
        tvPostClothStockCommit.setOnClickListener(this);



    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_post_cloth_stock_choose_photo:
                Intent intent=new Intent(getActivity(),ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.ll_post_cloth_stock_time:
                new DateTimePickDialogUtil(getActivity()).dateTimePicKDialog(tvPostClothStockTime);
                break;
            case R.id.ll_choose_style_name:
                String timeline = Utils.getTimeline();
                String sign = Utils.md5(timeline);
                ApiManage.getClothStockType(this, timeline, sign, Utils.getUserId(getActivity(), "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean dialogBean) {
                        if (Utils.checkResultCode(dialogBean.getReturn_code())) {
                            List<String> titles = new ArrayList<>();
                            for (int i = 0; i <dialogBean.getData().size(); i++) {
                                titles.add(dialogBean.getData().get(i).getTitle());
                            }
                            DialogAdapter dialogAdapter = new DialogAdapter(getActivity(), titles);
                            DialogUtil.showDialog(getActivity(), dialogAdapter, new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
//                                    T.showShort(getActivity(), item.toString());
                                    tvChooseStyleName.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
//
                break;
            case R.id.tv_post_cloth_stock_commit:

                if (files == null || files.length == 0) {
                    T.showShort(getActivity(), "请选择照片");
                } else {
                    String style = tvChooseStyleName.getText().toString();
                    String brand=etPostClothStockBrand.getText().toString();
                    String fabric = etPostClothStockFabric.getText().toString();
                    String size = etPostClothStockSize.getText().toString();
                    String color = etPostClothStockColor.getText().toString();
                    String price = etPostClothStockPrice.getText().toString();
                    String num = etPostClothStockNum.getText().toString();
                    String country = etPostClothStockCountry.getText().toString();
                    String date = tvPostClothStockTime.getText().toString();
                    String backup = etPostClothStockBackup.getText().toString();
                    if (Utils.isEmpty(style)) {
                        T.showShort(getActivity(), "请选择款式名称");
                    } else if (Utils.isEmpty(brand)) {
                        T.showShort(getActivity(), "请输入品牌");
                    } else if (Utils.isEmpty(fabric)) {
                        T.showShort(getActivity(), "请输入面料成分");
                    } else if (Utils.isEmpty(size)) {
                        T.showShort(getActivity(), "请输入尺码类别");
                    } else if (Utils.isEmpty(color)) {
                        T.showShort(getActivity(), "请输入颜色名称");
                    } else if (Utils.isEmpty(price)) {
                        T.showShort(getActivity(), "请输入目标价格");
                    }else if (Utils.isEmpty(num)) {
                        T.showShort(getActivity(), "请输入产品数量");
                    } else if (Utils.isEmpty(country)) {
                        T.showShort(getActivity(), "请输入原销售国家");
                    }else if (Utils.isEmpty(date)) {
                        T.showShort(getActivity(), "请选择生产年份");
                    } else if (Utils.isEmpty(backup)) {
                        T.showShort(getActivity(), "请输入其他说明");
                    } else {
                        Loading.show(getActivity(), getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }

    private void commit() {
        String buildDate= TimeUtils.dataOne(tvPostClothStockTime.getText().toString());
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_CLOTH_STOCK, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(getActivity(),e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(getActivity(), response);
                }
            },files,fileKeys,getParams(tvChooseStyleName.getText().toString(),etPostClothStockBrand.getText().toString(),etPostClothStockFabric.getText().toString(),
                    etPostClothStockSize.getText().toString(),etPostClothStockColor.getText().toString(),
                    etPostClothStockPrice.getText().toString(),etPostClothStockNum.getText().toString(),etPostClothStockCountry.getText().toString(),
                    buildDate,etPostClothStockBackup.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public OkHttpClientManager.Param[] getParams(String style, String brand, String fabric, String size, String color, String price,
                                                 String num, String  country, String builddate,String backup){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(getActivity(),"id")),
                new OkHttpClientManager.Param("style", style),
                new OkHttpClientManager.Param("brand", brand),
                new OkHttpClientManager.Param("fabric", fabric),
                new OkHttpClientManager.Param("size", size),
                new OkHttpClientManager.Param("color", color),
                new OkHttpClientManager.Param("price", price),
                new OkHttpClientManager.Param("num", num),
                new OkHttpClientManager.Param("country", country),
                new OkHttpClientManager.Param("builddate", builddate),
                new OkHttpClientManager.Param("backup", backup)
        };
    }
}
